package com.sunday.player.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.sunday.player.R;
import com.sunday.player.domain.domain_folder;



public class adapter_folder<T> extends ArrayAdapter<T> {

    private static View itemView;
    private LayoutInflater LI;
    public adapter_folder(Context context, int textViewResourceId,List<T> objects) {
	super(context, textViewResourceId, objects);
	LI = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
	ViewHolder holder = null;
	if(convertView == null){
	    itemView = convertView;
	    itemView = LI.inflate(R.layout.item_folder, null);
	    holder = new ViewHolder();
	    holder.IV_head	= (ImageView)itemView.findViewById(R.id.imageView1);
	    holder.CB_select 	= (CheckBox)itemView.findViewById(R.id.checkBox_folder);
	    holder.TV_foldername= (TextView)itemView.findViewById(R.id.textView_FolderName);
	    holder.TV_Counter	= (TextView)itemView.findViewById(R.id.textView_Count);
	    itemView.setTag(holder);
	}else{
	    holder = (ViewHolder)itemView.getTag();
	}
	
	final domain_folder folder  = (domain_folder)getItem(position);	
	holder.CB_select.setOnCheckedChangeListener(null);
	holder.CB_select.setChecked(folder.isCheck());
	holder.CB_select.setOnCheckedChangeListener(new OnCheckedChangeListener() {
	    
	    @Override
	    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		folder.setCheck(isChecked);
	    }
	});
	
	
	holder.TV_foldername.setText(folder.getTitle());
	holder.TV_Counter.setText(folder.getCounttoString());
	return itemView;
    }
    private static class ViewHolder{
	ImageView IV_head;
	CheckBox CB_select;
	TextView TV_foldername;
	TextView TV_Counter;
    }
}
